package easyjava.function;

import java.util.ArrayList;
import java.util.List;

/**
 * 执行流方法
 *
 */
public interface ExecFlowFunction {

	/**
	 * 顺序执行，逐个执行给定的action
	 */
	static Runnable sequence(Runnable... actions) {
		if(actions==null) return ()->{};
		return ()->{
			for(Runnable action:actions) {
				action.run();
			}
		};
	}
	
	/**
	 * 并行执行，同时执行给定的action
	 */
	static Runnable parallel(Runnable... actions) {
		if(actions==null) return ()->{};
		return ()->{
			List<Thread> actionThreadList=new ArrayList<>(actions.length);
			for(Runnable action:actions) {
				Thread actionThread=new Thread(action);
				actionThread.start();
				
				actionThreadList.add(actionThread);
			}
			for(Thread actionThread:actionThreadList) {
				try {
					actionThread.join();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		};
	}
}
